from lcopa import uphi

PARA_START = ";para start\r\n"
ELECTRODENO = "ELECTRODENO"

ELECTRODEWIDTH = "ELECTRODEWIDTH"
ELECTRODEGAP = "ELECTRODEGAP"
ELECTRODELENGTH = "ELECTRODELENGTH"
THICKNESS = "THICKNESS"
LIQUIDCRYSTAL = "LIQUIDCRYSTAL"
PARA_END = ";para end\r\n"
UPHI_START = ";u-phi start\r\n"
UPHI_END = ";u-phi end\r\n"

class u_phi:
    def __init__(self,file_name):
        self.file_name= file_name
        self.U_PHI = []
        self.ElectrodeNo = 1920
        self.load_uphi(file_name)
    def load_uphi(self,file_name=''):
        if((self.file_name != '')):
            print "u=phi name",file_name
            file_object = open(self.file_name)
            flag = 0
            try:
                line = file_object.readline( )
                if(line == PARA_START):
                    inter_line = ""
                    while(True):
                        inter_line = file_object.readline( )
                        print "inter_line",inter_line
                        if(inter_line == PARA_END):
                            break;
                        else:
                            key,val = (inter_line[:-3]).split("=")
                            if((key== ELECTRODENO)):
                                self.ElectrodeNo = int(val)
                            elif(key== ELECTRODEWIDTH):
                                self.ElectrodeWidth = float(val)
                            elif(key== ELECTRODEGAP):
                                self.ElectrodeGap = float(val)
                            elif(key==ELECTRODELENGTH):
                                self.ElectrodeLength = float(val)
                            elif(key == THICKNESS):
                                self.THICKNESS = float(val)
                            elif(key==LIQUIDCRYSTAL):
                                pass
                    line = file_object.readline( )
                    # print "line",line
                    if(line == UPHI_START):
                        while(True):
                            inter_line = file_object.readline( )
                            # print "inter_line",inter_line
                            if(inter_line == UPHI_END) or(inter_line == UPHI_END[:-2]):
                                break;
                            else:
                                key,val = (inter_line[:-2]).split('\t')
                                if key == 'U':
                                    pass
                                else:
                                    self.U_PHI.append(float(val))
                    return_val= 1
                else:
                    return_val = -1
                    self.file_name= ""
            except Exception,e:
                print "error",e
                return_val = -1
            finally:
                file_object.close( )
                return return_val
        else:
            return 1
    def is_exit(self,filename):
        return True
    def delete_file(self,name):
        pass
    def get_list(self):
        pass
    def toString(self):
        strn = "ElectrodeNo:%f\nElectrodeWidth:%f\nElectrodeGap:%f\nElectrodeLength:%f\n"%(self.ElectrodeNo,self.ElectrodeGap,self.ElectrodeLength,self.ElectrodeWidth)
        strn += str(self.U_PHI)
        return strn
